package com.wyl.home.timetable

import android.content.Intent
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.*
import com.wyl.base.bean.VideoData
import com.wyl.easydialog.EasyDialog
import com.wyl.easydialog.ViewHolder
import com.wyl.easydialog.ViewListener
import com.wyl.home.R
import com.wyl.home.databinding.TimeTableActivityBinding
import com.wyl.home.databinding.TimeTableItemBinding
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.openActivity
import com.wyl.libbase.extend.toast
import io.ditclear.bindingadapter.BindingViewHolder
import io.ditclear.bindingadapter.ItemClickPresenter
import io.ditclear.bindingadapter.ItemDecorator
import io.ditclear.bindingadapter.SingleTypeAdapter
import org.greenrobot.eventbus.EventBus
import org.koin.android.viewmodel.ext.android.viewModel

@Route(path = TimetablePage)
class TimeTableActivity : BindingActivity<TimeTableActivityBinding>(),
    ItemClickPresenter<VideoData>, ItemDecorator {

    private val viewModel: TimeTableViewModel by viewModel()

    override fun getLayoutId(): Int = R.layout.time_table_activity

    private val mAdapter1 by lazy {
        SingleTypeAdapter(
            this,
            R.layout.time_table_item,
            viewModel.week
        ).apply {
            itemDecorator = this@TimeTableActivity
        }
    }

    private val mAdapter2 by lazy {
        SingleTypeAdapter(
            this,
            R.layout.time_table_item,
            viewModel.nextWeek
        ).apply {
            itemDecorator = this@TimeTableActivity
        }
    }

    override fun onItemClick(v: View, item: VideoData) {
        if (v.id == R.id.tv_video || v.id == R.id.iv_video) {
            viewModel.collect(item)
            if (ACacheHelper.cache.getAsObject(PUSH) as Boolean? != true) {
                showNotifyDialog()
            }
        } else {
            if (item.appVideoId == "1") {
//                toast("暂无该剧集")
                return
            }
            if (item.playType != 1) {
                toast("该集尚未更新")
                return
            }
            openVideoDetailPage(item.appFolkOperaId, item.appVideoId)
        }
    }

    override fun decorator(
        holder: BindingViewHolder<ViewDataBinding>,
        position: Int,
        viewType: Int
    ) {
        val itemBinding = holder.binding as TimeTableItemBinding
        itemBinding.presenter = this@TimeTableActivity
//        if (itemBinding.item.type == "1") {
//            //本周
//        } else {
//            //下周
//
//        }
//        itemBinding.item
    }

    private val mLayoutManager by lazy { LinearLayoutManager(this) }
    private val mLayoutManager2 by lazy { LinearLayoutManager(this) }

    override fun initView() {
        binding.vm = viewModel

        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }

        binding.recyclerview1.apply {
            layoutManager = mLayoutManager
            adapter = mAdapter1
        }

        binding.recyclerview2.apply {
            layoutManager = mLayoutManager2
            adapter = mAdapter2
        }

    }

    override fun loadData() {
//         获取不到registrationID
//        val rid = JPushInterface.getRegistrationID(this)
//        KLog.d("rid = $rid")

        viewModel.success.observe(this, Observer {
            EventBus.getDefault().post(EVENT_LOGIN)
            finish()
        })

        // 注册成功和失败一样的处理
        viewModel.error.observe(this, Observer {
            toast(it ?: "")
        })

    }


    private fun showNotifyDialog() {
        EasyDialog.Builder()
            .setLayoutId(R.layout.time_table_notify_dialog)
            .setViewLisenter(object : ViewListener() {
                override fun convert(helper: ViewHolder, dialog: EasyDialog) {
                    helper.apply {
                        setOnClickListener(R.id.tv_cancel, View.OnClickListener {
                            dialog.dismiss()
                        })
                        setOnClickListener(R.id.tv_ok, View.OnClickListener {
                            dialog.dismiss()
                            viewModel.push()
                        })
                    }
                }
            })
            .setMargin(16)
            .setOutCancelable(false)
            .show(supportFragmentManager)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_hot_play -> startActivity(Intent(this, HotPlayActivity::class.java))
            R.id.home -> {
                openActivity(HomePage)
                finish()
            }
            else -> {
            }
        }
    }

}
